<?php
require_once "xpface/util/PostData.class.php";
require_once "xpface/util/XPFaceConfigLoader.class.php";
require_once "pworks/mvc/IFilter.iface.php";
require_once "pworks/common/httputil/DefaultHttpInputValidation.class.php";

class CheckLoginFilter implements IFilter {
	public function before(IAction &$action) {
        $page_id = DefaultHttpInputValidation::getDate(HttpConst::TYPE_REQUEST, "page_id");
        //xpface config
        $xpFaceConfigLoader = XPFaceConfigLoader::getInstance();
        $xpFaceConfig = $xpFaceConfigLoader->getXPFaceConfig();
        //var_dump($xpFaceConfig);
        
        //page config
        $pageConfig = $xpFaceConfigLoader->getPageConfig();
        //var_dump($pageConfig);
        $_config = $pageConfig[$page_id];
        //var_dump($_config);
        if ($_config->login == "yes") {
            $checkLoginApi = str_replace("#API_NAME#", "CheckLogin", WS_SERVICE);
            $result = PostData::post($checkLoginApi, array());
            $dataobj = json_decode($result);
            //var_dump($checkLoginApi);
            if (is_object($dataobj) && $dataobj->status == "exception") {
                if (get_class($action) == "XPFaceAction") {
                    header("Location: login.html");
                    exit;
                }
                echo "<script>location = \"login.html\";</script>";
                exit;
            }
        }
        return null;
	}

	public function after(IAction &$action) {
		return null;
	}
}
